{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "41701067",
   "metadata": {},
   "source": [
    "* 记录人：莫秀芳\n",
    "* 时间：2021-12-6\n",
    "* week14\n",
    "* 学习线上资源：\n",
    "[python官方文档](https://www.python.org/)  \n",
    "[智超的博客--函数](https://www.cnblogs.com/xuzhichao/p/11570285.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c19f27de",
   "metadata": {},
   "source": [
    "回顾上节课"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f68bcebf",
   "metadata": {},
   "source": [
    "函数的定义：def"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f337808",
   "metadata": {},
   "source": [
    "函数的名字尽量与变量的名字不要重复"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35eefcca",
   "metadata": {},
   "source": [
    "参数决定了函数的数据的输入 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4cd3580",
   "metadata": {},
   "source": [
    "在定义函数的参数，圆括号里面的叫形式参数  \n",
    "在调用函数的参数，圆括号里面的叫实际参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4487bc29",
   "metadata": {},
   "source": [
    "先后顺序的叫位置参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b443191",
   "metadata": {},
   "source": [
    "关键字参数不需要考虑顺序，它的限制条件是放在末尾。因为它指定赋值了，多个不考虑顺序。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7ba0ccd",
   "metadata": {},
   "source": [
    "参数：  \n",
    "  (1）形式参数  \n",
    "（2）实际参数  \n",
    "（3）有多个参数时：①关键字参数 ②位置参数（调用函数时）--》关键字参数均要放在末尾（多个不考虑顺序） ③默认参数（定义函数时）默认参数均要放在末尾（多个不考虑顺序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "038d0be1",
   "metadata": {},
   "source": [
    "# 本周知识\n",
    "> 1.函数的返回值  \n",
    "> 2.函数的参数类型  \n",
    "> 3.模块与函数的关系  \n",
    "> 4.定义一个模块，然后调用模块  \n",
    ">> A.调用模块  \n",
    ">> B.调用模块中的函数    \n",
    "\n",
    "> 5.模块与函数实践  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45808482",
   "metadata": {},
   "source": [
    "# 1.函数的返回值\n",
    "\n",
    "> 1. 函数一定有返回值 （无论是什么编程语言）\n",
    "> 2. 在其他的编程语言中（如c语言，java等），返回值一定要写，不写会报错！；对于python（更高级的语言），返回值如果不写，给定默认值 None\n",
    "> 3. 返回值一般为函数体运行的结果（result):也可以是自定义的函数返回值(实践2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "070d54d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践1  \n",
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+ last_name\n",
    "#     print(full_name)\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ca76f6a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Li Hua'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_formatted_name(\"Li\",\"Hua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "485ae5e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+ last_name\n",
    "#     print(full_name)\n",
    "    return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "31bc85dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"Li\",\"Hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4d519f56",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践2  \n",
    "def log_in(username,password):\n",
    "    \"\"\"验证登录信息\"\"\"\n",
    "    if username == \"Lihua\" and password == 123456:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "92b1907d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"Lihua\",12345)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36ed145e",
   "metadata": {},
   "source": [
    "## 2.函数的参数\n",
    "### 1.函数输入的参数\n",
    "> 1.函数可以输入复杂的参数：\n",
    ">> A.函数参数为列表  \n",
    ">> B.函数参数为字典\n",
    "\n",
    "> 2.函数可以输入不确定个数的参数：  \n",
    "> 1.*args   \n",
    ">> 1.args 可以接受多个数据（单值）  \n",
    ">> 2.args 将将多个数据存在一个**元组**当中，实际还是一个变量  \n",
    "> 2. ** kwargs\n",
    ">> 1.kwargs可以接受多个数据（键值对）==> 关键字参数\n",
    ">> 2.kwargs 将多个键值对  的数据存放在一个 **字典** 中，实际上还是一个变量\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "c94b8738",
   "metadata": {},
   "outputs": [],
   "source": [
    "def food_ingred(food,*ingreds):\n",
    "    \"\"\"食物配料单\"\"\"\n",
    "    return food,ingreds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "bc2f7b8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('green peppers', 'extra cheese'))"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "food_ingred(\"pizza\",\"green peppers\",\"extra cheese\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "31c4b316",
   "metadata": {},
   "outputs": [],
   "source": [
    "# **kwargs\n",
    "def person(stud_name,**stud_info):\n",
    "    \"\"\"学生个人信息完善表\"\"\"\n",
    "    return stud_name,stud_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "7766a9e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Lihua', {'arg': 18, 'job': 'student', 'address': '广州南方学院'})"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person(\"Lihua\",arg=18,job=\"student\",address=\"广州南方学院\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "8ef7bac1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def func_name(argl,arg2,*arg,**kwargs):\n",
    "    \"\"\"考虑比较周到的函数参数\"\"\"\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "5cabb8d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_test(names):\n",
    "    \"\"\"测试输入参数为列表\"\"\"\n",
    "    print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "5e95c017",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Lihua', 'Xiaofeng', 'Ziqi']\n"
     ]
    }
   ],
   "source": [
    "list_test([\"Lihua\",\"Xiaofeng\",\"Ziqi\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c1456b7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dict_test(request_header):\n",
    "    \"\"\"测试输入参数为字典\"\"\"\n",
    "    print(request_header)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "dab48329",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 只要你是完整的独立的数据，都可以接收\n",
    "# https;//httpbin.org/get\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "772944e8",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'httpbin_get' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-33-de707429c3e9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdict_test\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhttpbin_get\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'headers'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'httpbin_get' is not defined"
     ]
    }
   ],
   "source": [
    "dict_test(httpbin_get['headers'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6c14cef",
   "metadata": {},
   "source": [
    "# 2.函数输出的返回值\n",
    "> 1.返回值可以是 字符串、数值、列表、字典等数据（单一值）\n",
    "\n",
    "> 2.返回值是唯一的，不可以有两个（这是其他语言，如c语言，Java等）\n",
    "\n",
    "> 3.（python是高级语言）python可以输出多个返回值，结果会用一个元组进行封装并返回。（本质不违背函数的定义）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "a3e337f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return \"你好\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "97ba5f5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "239638e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "d171586f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "bb6b5076",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return [1,2,3,4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "a27c0df4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44fddbbf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "69a2d736",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试多个值\n",
    "def test5_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return 1,2,\"Lihua\",[1,2,3],{\"name\":\"Alex\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "ac39d58c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 'Lihua', [1, 2, 3], {'name': 'Alex'})"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test5_return()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21b75607",
   "metadata": {},
   "source": [
    "in是输入    out是结果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e4bfaeb",
   "metadata": {},
   "source": [
    "# 3.模块\n",
    "> 1.使用模块可以隐藏函数的源代码  \n",
    "> 2.使用的人不需要会底层代码 （只需要会使用模块即可）\n",
    "> 调用模块的方法  \n",
    "> A.必须先调用模块，才可以使用\n",
    "> a.import moudle_name 直接调用整个模块，此时模块的所有函数处于备用状态，使用的方法为：moudle_name.func_name()  \n",
    "> b.from moudle_name import func_name,此时，仅调用模块的某一函数，只能使用这个模块的该函数，使用方法为func_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "55935435",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "1ec174ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=12, tm_mday=6, tm_hour=16, tm_min=35, tm_sec=47, tm_wday=0, tm_yday=340, tm_isdst=0)"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d0c83453",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调用模块中函数的方法一，调用整个模块\n",
    "import study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5e8dff1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "study.search4vowels(\"hello zhichao\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3e6b7ef",
   "metadata": {},
   "source": [
    "函数名不能是内部使用过的名字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "094cb9f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#study.news_链接（’https://www.nfu.edu.cn/','xydt','96'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8179ee4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调用模块中函数的方法二，调用模块中某一个函数\n",
    "from study import search4vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "eb426ebf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels(\"bfejkwqtrq2wqcdas\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a9dfbf2",
   "metadata": {},
   "source": [
    "“.py”就是默认用pycharm打开\n",
    "\n",
    "就算是在jupyter notebook里的Text File文本"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f84c77d",
   "metadata": {},
   "source": [
    "去看第五章的flask"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
